package com.guoxin.concurrentprogramming.chapter3.threadpool;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author guoxin
 * @date 2019/7/16
 */
public class ThreadFactoryDemo {
    private static class MyTask implements Runnable {
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0L,
                TimeUnit.MICROSECONDS, new SynchronousQueue<>(), r -> {

            Thread thread = new Thread(r);
            thread.setDaemon(true);
            System.out.println("create " + thread);
            return thread;
        });
        MyTask myTask = new MyTask();
        for (int i = 0; i < 5; i++) {
            executor.submit(myTask);
        }

        Thread.sleep(200);
    }
}
